import {queryAttr, queryGoodsTypeName,addAttr, modAttr, delAttr,queryAll} from '../services/goods';

export default {
    namespace: 'attribute',

    state: {
        data: {
            list: [],
            pagination: {},
        },
        loading: true,
        record:{},
    },

    effects: {
        * fetch({payload}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryAttr,payload);
            yield put({
                type: 'save',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
        },

        * del({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(delAttr, payload);
            yield put({
                type: 'changeLoading',
                payload: false,
            });

            if (callback) callback(response);
        },
        * queryAll({payload, callback}, {call, put}) {
            yield put({
                type: 'changeLoading',
                payload: true,
            });
            const response = yield call(queryAll,payload);
            yield put({
                type: 'save',
                payload: response,
            });
            yield put({
                type: 'changeLoading',
                payload: false,
            });
            if (callback) callback(response);
        },
        * handleRecords({payload},{call,put}){
            yield put({
                type:'handleRecord',
                payload:payload,
            })
        },
    },

    reducers: {
        save(state, action) {
            return {
                ...state,
                data: action.payload.resultData.attrList,
            };
        },

        changeLoading(state, action) {
            return {
                ...state,
                loading: action.payload,
            };
        },
        handleRecord(state,{payload}){
            return {
                ...state,
                record:payload,
            }
        },
    },
};
